package com.bw.module_detail.viewmodel

import androidx.lifecycle.viewModelScope
import com.bw.lib_common.base.BaseViewModel
import com.bw.module_detail.intent.DetailIntent
import com.bw.module_detail.model.repository.DetailRepository
import com.bw.module_detail.state.DetailSimpleState
import dagger.hilt.android.lifecycle.HiltViewModel
import com.bw.module_detail.model.entity.CommentEntity
import com.bw.module_detail.model.entity.DanMuEntity
import com.bw.module_detail.model.entity.VideoEntity
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.consumeAsFlow
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.launch
import javax.inject.Inject

/**
 * @author ykj
 * @date 2025-10-22 08:57
 * @description
 */
@HiltViewModel
class DetailViewModel @Inject constructor(private val repository: DetailRepository):BaseViewModel<DetailIntent>(){
    //评论列表
    private var commentState = MutableStateFlow<DetailSimpleState<List<CommentEntity>>>(DetailSimpleState.Init)
    val commentState_ = commentState.asStateFlow()
    //发送评论
    private var publishCommentState = MutableStateFlow<DetailSimpleState<CommentEntity>>(DetailSimpleState.Init)
    val publishCommentState_ = publishCommentState.asStateFlow()
    //视频列表
    private var videoState = MutableStateFlow<DetailSimpleState<List<VideoEntity>>>(DetailSimpleState.Init)
    val videoState_ = videoState.asStateFlow()
    //弹幕列表
    private var bulletScreenState  = MutableStateFlow<DetailSimpleState<List<DanMuEntity>>>(DetailSimpleState.Init)
    val bulletScreenState_ = bulletScreenState.asStateFlow()
    //发布弹幕
    private var publishBulletScreenState  = MutableStateFlow<DetailSimpleState<DanMuEntity>>(DetailSimpleState.Init)
    val publishBulletScreenState_ = publishBulletScreenState.asStateFlow()
    //修改金币
    private var modifyCoinAmountState  = MutableStateFlow<DetailSimpleState<Boolean>>(DetailSimpleState.Init)
    val modifyCoinAmountState_ = modifyCoinAmountState.asStateFlow()
    override fun handleIntent() {
        //根据不同的意图处理不同的数据请求
        viewModelScope.launch {
            intents.consumeAsFlow().collect{
                when(it){
                    is DetailIntent.getVideoList -> {
                        getVideoList(it)
                    }
                    is DetailIntent.getCommentList -> {
                        getCommentList(it)
                    }
                    is DetailIntent.getBulletScreenInfo -> {
                        getBulletScreenInfo(it)
                    }
                    is DetailIntent.publishBulletScreen ->{
                        //发布弹幕
                        publishBullet(it)
                    }
                    is DetailIntent.modifyCoinAmount ->{
                        //修改金币
                        modifyCoinAmount(it)
                    }
                    is DetailIntent.publishComment ->{
                        //发布评论
                        publishComment(it)
                    }
                }
            }
        }
    }
    private fun modifyCoinAmount(it: DetailIntent.modifyCoinAmount) {
        viewModelScope.launch {
            repository.modifyCoinAmount(it.accountId,it.nm)
                .catch {
                    modifyCoinAmountState.value = DetailSimpleState.Error(it.message.toString())
                }.filter {
                    it != null
                }.collect{
                    if (it.code == 200){
                        if (it.data){
                            modifyCoinAmountState.value = DetailSimpleState.Success(it.data)
                        }else{
                            modifyCoinAmountState.value = DetailSimpleState.Empty
                        }
                    }else{
                        modifyCoinAmountState.value = DetailSimpleState.Error(it.msg)
                    }
                }
        }
    }
    private fun publishBullet(intent: DetailIntent.publishBulletScreen){
        viewModelScope.launch {
            repository.publishBulletScreen(intent.entity)
                .catch {
                    publishBulletScreenState.value = DetailSimpleState.Error(it.message.toString())
                }
                .filter {
                    it != null
                }.collect{
                    if (it.code == 200){
                        if (it.data != null){
                            publishBulletScreenState.value = DetailSimpleState.Success(it.data)
                        } else{
                            publishBulletScreenState.value = DetailSimpleState.Empty
                        }
                    } else {
                        publishBulletScreenState.value = DetailSimpleState.Error(it.msg)
                    }
                }
        }
    }
    private fun getBulletScreenInfo(intent: DetailIntent.getBulletScreenInfo){
        viewModelScope.launch {
            repository.getBulletScreenInfo(intent.datatype, intent.itemId)
                .catch {
                    bulletScreenState.value = DetailSimpleState.Error(it.message.toString())
                }.filter {
                    it != null
                }.collect{
                    if (it.code == 200){
                        if (it.data.isNotEmpty()){
                            bulletScreenState.value = DetailSimpleState.Success(it.data)
                        }else{
                            bulletScreenState.value = DetailSimpleState.Error(it.msg)
                        }
                    }else{
                        bulletScreenState.value = DetailSimpleState.Error(it.msg)
                    }
                }
        }
    }

    private fun getVideoList(it:DetailIntent.getVideoList){
        viewModelScope.launch {
            repository.getVideoList(it.page,it.pagesize)
                .catch {
                    videoState.value = DetailSimpleState.Error(it.message.toString())
                }.filter {
                    it != null
                }.collect{
                    if (it.code == 200){
                        if (it.data.isNotEmpty()){
                            videoState.value = DetailSimpleState.Success(it.data)
                        } else{
                            videoState.value = DetailSimpleState.Error(it.msg)
                        }
                    }else {
                        videoState.value = DetailSimpleState.Error(it.msg)
                    }
                }
        }
    }
    private fun getCommentList(it: DetailIntent.getCommentList){
        viewModelScope.launch {
            repository.getCommentList(it.datatype,it.itemid)
                .catch {
                    commentState.value = DetailSimpleState.Error(it.message.toString())
                }
                .filter {
                    it != null
                }
                .collect{
                if (it.code == 200){
                    if (it.data.isNotEmpty()){
                        commentState.value = DetailSimpleState.Success(it.data)
                    } else{
                        commentState.value = DetailSimpleState.Error(it.msg)
                    }
                } else {
                    commentState.value = DetailSimpleState.Error(it.msg)
                }
            }
        }
    }

    //发送评论
    private fun publishComment(intent:DetailIntent.publishComment){
        viewModelScope.launch {
            repository.publishComment(intent.body)
                .catch {
                    publishCommentState.value = DetailSimpleState.Error(it.message.toString())
                }
                .filter {
                    it != null
                }
                .collect{
                    if (it.code == 200){
                        if (it.data != null){
                            publishCommentState.value = DetailSimpleState.Success(it.data)
                        } else{
                            publishCommentState.value = DetailSimpleState.Error(it.msg)
                        }
                    } else {
                        publishCommentState.value = DetailSimpleState.Error(it.msg)
                    }
                }
        }
    }
}